using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;


namespace PocketLibrary.Media
{

    public class MediaHistoryClass
    {
        const int HistoryItemWidth = 64;
        enum AnchorPosition
        {
            Top,
            Bottom,
            Left,
            Right,
            Center,
        }

        Dictionary<string, MediaHistoryItem> history = null;

        private string mediahistoryfolder;

        public string MediaHistoryFolder
        {
            get { return mediahistoryfolder; }
            set { mediahistoryfolder = value; }
        }


        public Dictionary<string, MediaHistoryItem> HistoryItems
        {
            get
            {
                return history;
            }
        }

        Bitmap ResizeImage(System.Drawing.Image image, int width)
        {
            int height;

            // measure the image and calculate size if it's more than width wide
            System.Drawing.Image baseImage = image;

            if (baseImage.Width > width)
            {
                float actualWidth = baseImage.Width;
                float actualHeight = baseImage.Height;
                float maxWidth = width;
                //width = width;
                height = Convert.ToInt32((maxWidth / actualWidth) * actualHeight);
            }
            else
            {
                width = baseImage.Width;
                height = baseImage.Height;
            }

            Bitmap thumbImage = new Bitmap(width, height);
            Graphics thumbgraphic = Graphics.FromImage(thumbImage);

            thumbgraphic.DrawImage(baseImage, new Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height,
                GraphicsUnit.Pixel, new ImageAttributes());

            baseImage.Dispose();
            return thumbImage;
        }

        Bitmap Crop(System.Drawing.Image imgPhoto, int Width, int Height, AnchorPosition Anchor)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
            {
                nPercent = nPercentW;
                switch (Anchor)
                {
                    case AnchorPosition.Top:
                        destY = 0;
                        break;
                    case AnchorPosition.Bottom:
                        destY = (int)
                            (Height - (sourceHeight * nPercent));
                        break;
                    default:
                        destY = (int)
                            ((Height - (sourceHeight * nPercent)) / 2);
                        break;
                }
            }
            else
            {
                nPercent = nPercentH;
                switch (Anchor)
                {
                    case AnchorPosition.Left:
                        destX = 0;
                        break;
                    case AnchorPosition.Right:
                        destX = (int)
                          (Width - (sourceWidth * nPercent));
                        break;
                    default:
                        destX = (int)
                          ((Width - (sourceWidth * nPercent)) / 2);
                        break;
                }
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width,
                    Height, PixelFormat.Format24bppRgb);
            //bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
            //        imgPhoto.VerticalResolution);


            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            //grPhoto.InterpolationMode =
            //        InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }

        public bool Store(PocketLibrary.IBlog blog, FileInfo file)
        {
            if (file.Extension.ToLower() == ".jpg" ||
                file.Extension.ToLower() == ".bmp" ||
                file.Extension.ToLower() == ".png")
            {
                //Bitmap sthumb = ResizeImage(new Bitmap(file.FullName), HistoryItemWidth);
                Bitmap sthumb = Crop(new Bitmap(file.FullName), HistoryItemWidth, HistoryItemWidth, AnchorPosition.Center);

                using (FileStream fs = new FileStream(MediaHistoryFolder + file.Name + "_history" + file.Extension, FileMode.CreateNew, FileAccess.Write))
                {
                    sthumb.Save(fs, ImageFormat.Jpeg);
                }

                sthumb.Dispose();

                using (FileStream fs = new FileStream(MediaHistoryFolder + file.Name + "_history.data", FileMode.CreateNew, FileAccess.Write))
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(blog.BlogName);
                    sw.WriteLine(blog.BlogId);
                    sw.WriteLine(blog.BlogUrl);
                    sw.WriteLine(MediaHistoryFolder + file.Name + "_history" + file.Extension);
                    sw.WriteLine(file.Name);
                    sw.WriteLine(DateTime.Now);
                }

                return true;
            }
            else
                return false;
        }

        public Dictionary<string, MediaHistoryItem> GetMedia(IBlog blog)
        {
            history = new Dictionary<string, MediaHistoryItem>();
            FileInfo[] datafiles = new DirectoryInfo(MediaHistoryFolder).GetFiles("*.data");

            foreach (FileInfo datafile in datafiles)
            {
                using (FileStream fs = datafile.OpenRead())
                using (StreamReader sr = new StreamReader(fs))
                {
                    string readname = sr.ReadLine();
                    if (readname == blog.BlogName)
                    {
                        MediaHistoryItem item = new MediaHistoryItem();
                        item.BlogName = blog.BlogName;
                        sr.ReadLine(); // blogid from file
                        item.BlogId = blog.BlogId;
                        sr.ReadLine(); // blogurl from file
                        string imagepath = sr.ReadLine(); // full path to image from file
                        item.Filename = imagepath;
                        item.Name = sr.ReadLine(); // image filename from file;

                        item.ItemBitmap = new Bitmap(imagepath);

                        item.BlogApiType = blog.Type; // blogtype from parameter

                        history.Add(item.Name, item);
                    }
                }
            }
            return history;

        }
    }
}
